import os
import time
import allure
import paramiko
import pytest

import page
from page.page_login import PageLogin
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("我的页面")
@allure.feature("登录测试用例")
class TestLogin:

    @classmethod
    def setup_class(cls):
        # 初始化driver对象，实例化page对象
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        # 使用实例化对象调用类方法
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
            # 正式站包跳过一键登录
            # cls.login.base_click_element(page.logIn_back_btn)
        except:
            pass

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @allure.story("正确的用户名和密码")
    @pytest.mark.parametrize("record_screen", [module_name], indirect=True)
    def test01_login(self, record_screen):
        # global ssh, path, filename
        # if os.environ['RUN_HOST'] != "http://127.0.0.1:4723/wd/hub":
        #     ip = os.environ['RUN_HOST'].split('/')[2].split(':')[0]
        #     ssh = paramiko.SSHClient()
        #     # 允许连接不在know_hosts文件中的主机
        #     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        #     # 连接服务器(本地ssh服务)
        #     ssh.connect(hostname=ip, port=2233, username='test', password='123456')
        #     # 执行命令
        #     path = r"D:\pytest_video" + os.sep + module_name + '-{}.mp4' \
        #         .format(time.strftime("%Y_%"
        #                               "m_%d-%H_%M_%S"))
        #     list2 = path.split('\\')
        #     list2.reverse()
        #     filename = list2[0]
        #     cmd = "scrcpy -r " + path
        #     print(cmd)
        #     ssh.exec_command(cmd)

        self.login.pagelogin('11100009001', '123456')
        log.info("正在进行断言")
        try:
            assert self.login.base_find_element(page.wd_username)
            log.info('断言成功')
            self.login.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.login.base_get_screenshot_as_file(module_name, '登录失败')
            log.error('登录失败')
            raise
        # finally:
        #     if os.environ['RUN_HOST'] != "http://127.0.0.1:4723/wd/hub":
        #         time.sleep(2)
        #         stdin1, stdout1, stderr1 = ssh.exec_command('taskkill /IM scrcpy.exe')
        #         # 获取命令结果
        #         result = stdout1.read().decode('gbk')
        #         print(result)
        #         time.sleep(2)
        #         # 传输文件到服务器本地
        #         stdin2, stdout2, stderr2 = ssh.exec_command(r'net use \\192.168.1.28\ipc$ "123456" /user:zaodao')
        #         result = stdout2.read().decode('gbk')
        #         print(result)
        #         stdin3, stdout3, stderr3 = ssh.exec_command(f'xcopy {path} \\\\192.168.1.28\shared /Y')
        #         result = stdout3.read().decode('gbk')
        #         print(result)
        #         time.sleep(10)
        #         stdin4, stdout4, stderr4 = ssh.exec_command(r'net use \\192.168.1.28\ipc$ /delete')
        #         result = stdout4.read().decode('gbk')
        #         print(result)
        #
        #         path_server = r'C:\Users\zaodao\Desktop\shared' + os.sep + filename
        #         with open(path_server, 'rb') as f:
        #             allure.attach(f.read(), f'录屏文件，文件名:{filename}', allure.attachment_type.MP4)
        #         time.sleep(2)
        #     else:
        #         pass
